import sys
from UI import Ui_MainWindow
import random
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox

from word import Word


# 从外部读入单词
def read_words():
    words_list = []  # 单词列表
    for line in open("words.txt", 'r', encoding='utf-8'):
        english = line.strip().split(":")[0]
        chinese = line.strip().split(":")[1]
        words_list.append(Word(english, chinese))
    return words_list


class Guess_the_word(QMainWindow, Ui_MainWindow):

    def __init__(self, parent=None):
        super(QMainWindow, self).__init__(parent)  # 继承主窗口类
        self.index = None
        self.position = 0  # 偏移量
        self.score = 0  # 得分
        self.words_list = read_words()  # 单词列表
        self.words_count = len(self.words_list)  # 单词总个数
        self.wrong_words_list = []  # 错误单词列表
        self.setupUi(self)
        self.display_world()  # 初始化文本
        self.initUi_button()  # 初始化按键

    def display_world(self):
        word = self.words_list[self.position]
        english = word.english
        chinese = word.chinese
        print(english)
        self.index = random.randint(0, len(english) - 1)
        t = list(english)
        t[self.index] = '_'
        temp = ''.join(t)
        # self.temp = word.replace(self.index, '_')
        self.label_1.setText(temp)  # 显示单词
        self.label_2.setText('请输入:')
        self.label_3.setText(str(self.score))
        self.label_4.setText(chinese)  # 显示汉语

    def initUi_button(self):
        self.pushButton.clicked.connect(self.abnormal)  # 确认
        self.pushButton_2.clicked.connect(self.init_display_world)  # 重新开始

    # 重新开始
    def init_display_world(self):
        self.position = 0  # 偏移量清零
        self.score = 0  # 得分清零
        self.wrong_words_list = []  # 错误单词列表清零
        print(self.position)
        self.display_world()

    def abnormal(self):
        QApplication.processEvents()  # 将正在处理的事件的控制权还给Qt 个人理解为把它丢到子进程，以防主进程堵塞
        text = self.lineEdit.text()  # 接受文本框中的文本
        self.guess(text, self.position)

    def guess(self, text, position):  # 判断
        position = self.position
        msgBox = QMessageBox()
        if self.words_list[position].english[self.index] == text:
            self.position += 1
            self.score += 1  # 得分
        else:
            self.wrong_words_list.append(self.position)  # 保存错误单词的索引位置
            msgBox.critical(self, "错误", "正确单词为:" + self.words_list[self.position].english)  # 弹出对话框
            self.position += 1

        if self.position < self.words_count:
            self.display_world()

        self.lineEdit.clear()  # 清除输入框

        if position == self.words_count - 1:  # 结束
            if len(self.wrong_words_list) == 0:
                msgBox.about(self, "结束", "恭喜你获得了满分！")  # 弹出对话框
            else:
                str = ":"
                print(self.wrong_words_list)
                for wrong_word in self.wrong_words_list:
                    str += self.words_list[wrong_word].english
                    str += "  "
                msgBox.about(self, "结束", "你猜错的单词有" + str)  # 弹出对话框


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = Guess_the_word()
    MainWindow.setWindowTitle('猜词小游戏')
    MainWindow.show()
    sys.exit(app.exec_())
